<!--
 * @Description: 学习监控
 * @Autor: dyx
 * @Date: 2023-02-17 09:10:34
 * @LastEditTime: 2023-02-25 09:27:12
-->
<template>
  <transition name="el-zoom-in-center">
    <div class="JADP-preview-main">
      <div class="JADP-common-page-header">
        <el-page-header @back="goBack" content="学习监控" />
        <div class="options">
          <el-button @click="goBack">取 消</el-button>
        </div>
      </div>

      <div class="JADP-common-page-main" v-loading="listLoading">
        <div class="onlinePopulation" v-if="list.length">
          <span>
            <span style="margin-right:10px"> {{ timerNum }} S后更新列表</span>
            <el-button type="text" icon="el-icon-refresh" @click="refresh"
              >立即刷新</el-button
            >
          </span>

          <span style="margin-left:300px">
            <el-tag style="margin-right:10px"
              >在线 {{ LineCountForm.onLineCount }}</el-tag
            >
            <el-tag type="danger">离线 {{ LineCountForm.offLineCount }}</el-tag>
          </span>
        </div>
        <ul v-if="list.length" class="learningRecord">
          <li v-for="(item, index) in 20" :key="index" @click="getInfo(item)">
            <div class="userName">
              <!-- <span>{{ item.studentname }}</span>
              <span
                class="isOnline"
                :style="item.online == 0 ? 'color:#f56c6c' : 'color: #82bffc'"
              >
                <i class="icon-ym icon-ym-generator-radio"></i>
                {{ item.online == "0" ? "离线" : "在线" }}</span
              > -->
            </div>
            <div class="photo">
              <!-- <img v-if="item.online == 1" :src="item.snapshots" alt="" />
              <img v-else src="@/assets/zazx/personnel.png" alt="" /> -->
            </div>
          </li>
        </ul>
        <el-empty class="empty" v-else description="暂无数据"></el-empty>
        <div class="pagingStyle">
          <pagination
            :total="total"
            :page.sync="listQuery.currentPage"
            :limit.sync="listQuery.pageSize"
            @pagination="initData(clazzId)"
          />
        </div>
      </div>
      <LearningForm v-if="LearningFormVisible" ref="LearningForm" />
    </div>
  </transition>
</template>
<script>
import request from "@/utils/request";
import LearningForm from "./Form.vue";
export default {
  name: "learningCenter",
  components: {
    LearningForm
  },
  data() {
    return {
      listLoading: false,
      LearningFormVisible: false,
      list: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: ""
      },
      timer: null,
      timerNum: 30,
      LineCountForm: {
        onLineCount: 0,
        offLineCount: 0
      }
    };
  },
  methods: {
    goBack() {
      this.$emit("refresh");
    },
    initData(id) {
      this.listLoading = true;
      this.clazzId = id;
      this.getOnlinePopulation();
      request({
        url: "/api/xy_kj_learn_center/Xy_kj_learn_center",
        method: "get",
        data: {
          classid: id,
          ...this.listQuery,
          organizeId:this.$store.getters.organizeId
        }
      }).then(res => {
        console.log(res);
        this.list = res.data.list;
        this.total = res.data.pagination.total;
        this.listLoading = false;
        // 触发定时器
        this.myTimerEvent();
      });
    },
    myTimerEvent() {
      this.timerNum = 30;
      this.timer = setInterval(() => {
        if (this.timerNum <= 0) {
          clearInterval(this.timer);
          this.initData(this.clazzId);
          this.timerNum = 30;
        }
        this.timerNum--;
      }, 1000);
    },
    refresh() {
      // 清除定时器
      clearInterval(this.timer);
      this.initData(this.clazzId);
    },
    //获取在线人数和离线人数
    getOnlinePopulation() {
      request({
        url: `api/xy_kj_learn_center/Xy_kj_learn_center/onOffLine/${
          this.clazzId
        }`,
        method: "get"
      }).then(res => {
        this.LineCountForm = res.data;
      });
    },
    getInfo(item) {
      this.LearningFormVisible = true;
      this.$nextTick(() => {
        this.$refs.LearningForm.init(item.id);
      });
    }
  },
  destroyed() {
    // 组件销毁时清除定时器
    clearInterval(this.timer);
  }
};
</script>
<style scoped lang="scss">

li {
  list-style: none;
}
.JADP-common-page-main {
  padding: 10px;
  padding-top: 0;
  .onlinePopulation {
    width: 100%;
    height: 50px;
    line-height: 50px;
    border-bottom: 1px solid #ccc;
    font-size: 14px;
    padding-left: 30px;
  }
  .learningRecord {
    width: 100%;
    height: 70vh;
    display: flex;
    flex-wrap: wrap;
    overflow-y: scroll;
    li {
      width: 15%;
      height: 250px;
      // background: #ccc;
      border: 2px solid #ccc;
      margin: 13px;
      box-sizing: border-box;
      cursor: pointer;
      .userName {
        position: relative;
        width: 100%;
        height: 30px;
        text-align: center;
        line-height: 30px;
        border-bottom: 1px solid #ccc;
        .isOnline {
          position: absolute;
          display: flex;
          align-items: center;
          justify-content: center;
          right: 20px;
          top: 0;
          font-size: 12px;
          i {
            margin-right: 5px;
          }
        }
      }
      .photo {
        width: 100%;
        height: 216px;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
  
  .empty {
    height: 650px;
  }
}
</style>
